import 'dart:developer';

import 'package:dio/dio.dart';

/// 网络请求与返回信息打印拦截器
/// 对特殊情况特定接口进行调试
class PrintLogInterceptor extends InterceptorsWrapper {
  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
    // 检查路径是否为 /device/cloudEvent
    if (options.uri.path == '/device/getMyList') {
      log("\nrequest-------------->");
      options.headers?.forEach((key, value) {
        log("请求头信息：key=$key  value=${value.toString()}");
      });
      log("path:${options.uri}");
      log("method:${options.method}");
      log("data:${options.data}");
      log("queryParameters:${options.queryParameters.toString()}");
      log("<--------------request\n");
    }

    // 继续处理请求
    super.onRequest(options, handler);
  }

  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) {
    /// 下面代码是响应结果打印
    /// 针对接口路径打印避免干扰
    // if (response.requestOptions.path.contains("/login")) {
    //   log("\nresponse-------------->");
    //   log("path:${response.realUri}");
    //   // log("headers:${response.headers.toString()}");
    //   response.headers.forEach((key, value) {
    //     log("请求头信息：key=$key  value=${value.toString()}");
    //   });
    //   log("statusMessage:${response.statusMessage}");
    //   log("statusCode:${response.statusCode}");
    //   log("extra:${response.extra.toString()}");
    //   log("data:${response.data}");
    //   log("<--------------response\n");
    // }

    super.onResponse(response, handler);
  }

  @override
  void onError(DioException err, ErrorInterceptorHandler handler) {
    // log("\nonError-------------->");
    // log("error:${err.toString()}");
    // log("<--------------onError\n");
    super.onError(err, handler);
  }
}
